package com.xiaoshi.translator;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.xiaoshi.utils.Base64Utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author sen
 * @description  base64 加密
 * @date 2023/4/26 11:04
 */

public class EncryptSerializer extends JsonSerializer<String> {
    private static final Logger log = LoggerFactory.getLogger(EncryptSerializer.class);

    public EncryptSerializer() {
    }

    public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException {
        jsonGenerator.writeString(this.replaceStr(value));
    }

    private String replaceStr(String str) {
        log.debug("执行序列化");
        return StringUtils.isBlank(str) ? str : Base64Utils.encodeToString(str);
    }
}